﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using classes;

namespace Forms
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void login(object sender, EventArgs e)
        {
            var valid = validateLogin(username_txt.Text, password_txt.Text);
            if (!valid)
            {
                MessageBox.Show("Please enter your login credentials");
            }
            else
            {
                checkAuthentication(username_txt.Text, password_txt.Text);
            }
        }

        private void checkAuthentication(string u, string p)
        {
            username_txt.Enabled = password_txt.Enabled = loginButton.Enabled = false;
            DB_sql db = new DB_sql();
            int cnt = db.Count("select count(*) from users where username='" + u + "' and password='" + p + "'");
            if (cnt <= 0)
            {
                MessageBox.Show("username/password invalid.");
            }
            else
            {
                this.Hide();
                mainForm orderForm = new mainForm();
                orderForm.ShowDialog();
            }
            username_txt.Enabled = password_txt.Enabled = loginButton.Enabled = true;
            username_txt.Text = password_txt.Text = "";
            username_txt.Focus();
        }

        private bool validateLogin(string username, string password)
        {
            bool valid = true;
            if (username.Length <= 0 || password.Length <= 0)
            {
                valid = false;
            }
            return valid;
        }

    }
}
